<template>
    <rx-layout style="padding: 10px">
        <template #middle>
            <rx-fit v-model="fitSearch">
                <template #head>
                    <div style="display: flex;align-items: center;justify-content: space-between;">
                        <div class="table-operator">
                            <a-button type="primary" @click="addInst">
                                <template #icon>
                                    <plus-outlined/>
                                </template>
                                {{ i18n('list.addInst', '加入机构') }}
                            </a-button>
                        </div>
                        <span class="search-btn" @click.stop="searchshow" @mousedown.stop><SearchOutlined/>{{
                                $ti18('rxList.leachBtn', '过滤')
                            }}</span>
                    </div>
                </template>
                <template #search>
                    <a-form :layout="colLayout">
                        <a-row :gutter="24">
                            <a-col :span="colSpan">
                                <a-form-item :label="i18n('form.nameCn','公司中文名')">
                                    <a-input :placeholder="i18n('form.nameCn','公司中文名','input')"
                                             v-model:value="urlConfig.queryParam.Q_NAME_CN__S_LK"/>
                                </a-form-item>
                            </a-col>
                            <a-col :span="colSpan">
                                <a-form-item :label="i18n('form.domain','公司域名')">
                                    <a-input :placeholder="i18n('form.domain','公司域名','input')"
                                             v-model:value="urlConfig.queryParam.Q_DOMAIN__S_LK"/>
                                </a-form-item>
                            </a-col>
                        </a-row>
                    </a-form>
                    <div class="list-search-toolbar">
                        <a-button type="primary" @click="search">
                            <SearchOutlined/>
                            {{ $ti18('rxList.searchBtn', '查询') }}
                        </a-button>
                        <a-button style="margin-left: 4px" @click="resetSearch">
                            <RedoOutlined/>
                            {{ $ti18('rxList.resetBtn', '重置') }}
                        </a-button>
                    </div>
                </template>
                <template #middle>
                    <rx-grid ref="table"
                             :hasPage="true"
                             :urlConfig="urlConfig"
                             :selectConfig="selectConfig"
                             url="/api-user/user/org/osInst/applyListData"
                             field="instId"
                             :columns="columns"
                    >
                        <template #status="{row}">
                            <a-tag :color="statusMap[row.status].color">
                                <span>{{ statusMap[row.status].text }}</span>
                            </a-tag>
                        </template>
                        <template #moreInstStatus="{row}">
                            <a-tag :color="moreInstStatusMap[row.moreInstStatus].color">
                                <span>{{ moreInstStatusMap[row.moreInstStatus].text }}</span>
                            </a-tag>
                        </template>
                        <template #action="{ row }">
                            <RxGridAction :size="2">
                                <div v-if="!row.present && row.moreInstStatus=='APPLY'" @click.stop="cancelApply(row)"
                                     :title="i18n('list.cancelApply','取消申请')">{{ i18n('list.cancelApply', '取消申请') }}
                                </div>
                            </RxGridAction>
                        </template>
                    </rx-grid>
                </template>
            </rx-fit>
        </template>
    </rx-layout>
</template>

<script>
import OsInstApi from '@/common/api/user/osInst'
import {BaseList, RxLayout, RxGrid, RxFit, Util} from '@lowcode/jpaas-base-lib'
import OsInstJoinInstEdit from "./OsInstJoinInstEdit.vue";

export default {
    name: 'OsInstShowCanApplyList',
    components: {
        RxLayout,
        RxGrid,
        RxFit
    },
    mixins: [BaseList],
    data() {
        return {
            urlConfig: {
                ajaxType: 'post',
                contentType: 'json',
                queryParam: {},
                dataField: 'result.data',
                totalField: 'result.totalCount'
            },
            selectConfig: {
                type: 'checkbox',
            },
            // 表头
            columns: [
                {
                    title: this.i18n('form.nameCn', '机构中文名'),
                    field: 'nameCn',
                    minWidth: 130
                },
                {
                    title: this.i18n('form.domain', '机构域名'),
                    field: 'domain',
                    minWidth: 120
                },
                {
                    title: this.i18n('form.status', '状态'),
                    field: 'status',
                    slots: {default: 'status'},
                    minWidth: 120
                },
                {
                    title: this.i18n('form.joinOrNot', '是否加入'),
                    field: 'moreInstStatus',
                    slots: {default: 'moreInstStatus'},
                    minWidth: 120
                },
                {
                    title: this.i18n('form.remarks', '备注'),
                    field: 'moreInstNote',
                    minWidth: 120
                },
                {
                    title: this.$ti18('rxList.action', '操作'),
                    minWidth: 120,
                    field: 'action',
                    slots: {default: 'action'}
                },
            ],
            moreInstStatusMap: {
                ENABLED: {color: 'green', text: this.i18n('joinMap.inUse', '在用')},
                APPLY: {color: 'blue', text: this.i18n('joinMap.processing', '申请处理中')},
                REFUSE: {color: 'red', text: this.i18n('joinMap.refused', '已拒绝')},
                DISABLED: {color: 'red', text: this.i18n('joinMap.disabled', '已禁用')}
            },
            statusMap: {
                ENABLED: {color: 'green', text: this.i18n('statusMap.enable', '启用')},
                DISABLED: {color: 'red', text: this.i18n('statusMap.disable', '禁用')},
                INIT: {color: 'orange', text: this.i18n('statusMap.init', '初始化')},
            }
        }
    },
    methods: {
        i18n(name, text, key) {
            return this.$ti18(name, text, "OsInstShowCanApply", key);
        },
        addInst() {
            var self = this;
            var conf = {
                curVm: this,
                shade: false,
                widthHeight: ['800px', '600px'],
                component: OsInstJoinInstEdit,
                title: this.i18n('list.addInstTitle', '选择加入的机构')
            };
            Util.open(conf, function (action) {
                self.refreshGrid();
            });
        },
        cancelApply(record) {
            this.quitOrCancel(record, 'cancel');
        },
        quitApply(record) {
            this.quitOrCancel(record, 'quit');
        },
        quitOrCancel(record, type) {
            var self = this;
            this.$confirm({
                title: this.$ti18('rxMsa.actionTips', '操作提示'),
                zIndex: 20000,
                okText: this.$ti18('rxForm.ackBtn', '确认'),
                cancelText: this.$ti18('rxForm.cancelBtn', '取消'),
                content: type == 'quit' ? this.i18n('msa.exitOrg', '退出所选机构') : this.i18n('msa.cancelTips', '确定取消所选机构的申请？'),
                onOk() {
                    OsInstApi.quitOrCancel(record.instId, record.moreInstStatus).then(res => {
                        if (type == 'quit') {
                            window.location.reload();
                        } else {
                            self.refreshGrid();
                        }
                    })
                },
                onCancel() {
                }
            })
        }
    }

}
</script>
